﻿using Customer.Domain;
using Customer.Domain.Dto;
using Customer.Infrstructre.ClientManager;
using Customer.Infrstructre.CusContactManager;
using Customer.Read.API.Applcation.Command.ClientCommand;
using MediatR;
using System.Diagnostics.Contracts;

namespace Customer.Read.API.Applcation.CommandHandler.ClientCommanfHandler
{
    /// <summary>
    /// 客户显示
    /// </summary>
    public class SelectClientsCommanfHandler : IRequestHandler<SelectClientsCommand, Page<ClientDto>>
    {
        private readonly IClientRepository _clientRep;
        private readonly ICusContactRepository _cusContactRep;
        public SelectClientsCommanfHandler(IClientRepository _clientRep, ICusContactRepository _cusContactRep)
        {
            this._clientRep = _clientRep;
            this._cusContactRep = _cusContactRep;
        }

        public async Task<Page<ClientDto>> Handle(SelectClientsCommand request, CancellationToken cancellationToken)
        {
            Page<ClientDto> page = new Page<ClientDto>();
            var list = (from a in await _clientRep.GetAllAsync()
                        join b in await _cusContactRep.GetAllAsync()
                        on a.CusContactId equals b.CusContactId
                        where a.IsDel == false
                        select new ClientDto
                        {
                            ClientId = a.ClientId,
                            ClientCode = a.ClientCode,
                            ClientName = a.ClientName,
                            Cfax = a.Cfax,
                            Landline = a.Landline,
                            Mailbox = a.Mailbox,
                            Cremark = a.Cremark,
                            Office_Id = a.Office_Id,
                            User_Id = a.User_Id,
                            CusContactId = b.CusContactId,
                            Ccontact = b.Ccontact,
                            Caddress = b.Caddress,
                            Cphone = b.Cphone,
                            CreatTime = b.CreatTime
                        }).ToList();
            if (!string.IsNullOrEmpty(request.Office_Id))
            {
                list = list.Where(t => t.Office_Id == request.Office_Id).ToList();
            }
            //if (request.User_Id>0)
            //{
            //    list = list.Where(t => t.User_Id == request.User_Id).ToList();
            //}
            //客户编号查询
            if (!string.IsNullOrEmpty(request.ClientCode))
            {
                list = list.Where(t => t.ClientCode == request.ClientCode).ToList();
            }
            //客户名称模糊查询
            if (!string.IsNullOrEmpty(request.ClientName))
            {
                list = list.Where(t => t.ClientName.Contains(request.ClientName)).ToList();
            }
            //客户电话查询
            if (!string.IsNullOrEmpty(request.Landline))
            {
                list = list.Where(t => t.Landline == request.Landline).ToList();
            }
            page.lcount = list.Count();
            page.pcount = (int)Math.Ceiling((request.PageIndex * 1.0) / request.PageSize);
            page.list = list.OrderBy(t => t.ClientId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;
        }
    }
}
